const DtkSdk = require('dtk-nodejs-api-sdk')
const { appKey, appSecret, multiAccounts } = require('../config/index')


const sdk = new DtkSdk({
    appKey,
    appSecret,
    checkSign: 2
})


const apiRequest = async (url, params) => {
    try {
        const res = await sdk.request(url, params)
        return res
    } catch (err) {
        // throw new Error(err)
        return null
    }
}

const multiApiRequest = async (url, startTime, endTime) => {
    const urlDetailList = multiAccounts.map(async (account) => {
        let params = {
            type: 3, // 订单时间查询类型 1：下单时间，2：完成时间（购买用户确认收货时间），3：更新时间
            key: items.JDKEY, // 京东联盟授权key
            startTime: startTime, // 开始时间 格式yyyy-MM-dd HH:mm:ss，与endTime间隔不超过1小时
            endTime: endTime, // 结束时间 格式yyyy-MM-dd HH:mm:ss，与startTime间隔不超过1小时
            version: 'v1.0.0'
        }
        let sdkReq = new dtkSdk({ appKey: items.appKey, appSecret: items.appSecret, checkSign: 2 });
        let selUrlDetail = await sdkReq.request(url, {
            method: "GET",
            form: params
        })
        return selUrlDetail
    })
    return urlDetailList
}

module.exports = {
    apiRequest,
    multiApiRequest
}